package com.ktg.mes.wm.service.impl;

import java.io.Serializable;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ktg.common.utils.DateUtils;
import com.ktg.common.utils.SecurityUtils;
import com.ktg.mes.md.domain.MdProductStatisticsBom;
import com.ktg.mes.unit.other.SQLListenerInterface;
import com.ktg.mes.unit.other.SQLListenerType;
import com.ktg.mes.unit.other.ZLServiceIml;
import com.ktg.common.core.domain.model.SqlResult;
import com.ktg.mes.wm.domain.GatherIssueZuZhuangInfo;
import com.ktg.mes.wm.domain.WmItemRecptLine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktg.mes.wm.mapper.WmIssueLineMapper;
import com.ktg.mes.wm.domain.WmIssueLine;
import com.ktg.mes.wm.service.IWmIssueLineService;

/**
 * 生产领料单行Service业务层处理
 *
 * @author yinjinlu
 * @date 2022-07-14
 */
@Service
public class WmIssueLineServiceImpl  extends ZLServiceIml<WmIssueLineMapper, WmIssueLine> implements IWmIssueLineService
{

    @Autowired
    private WmItemRecptLineServiceImpl wmItemRecptLineService;

    /**
     * 查询生产领料单行
     *
     * @param lineId 生产领料单行主键
     * @return 生产领料单行
     */
    @Override
    public WmIssueLine selectWmIssueLineByLineId(Long lineId)
    {
        return getById(lineId);
    }

    /**
     * 查询生产领料单行列表
     *
     * @param wmIssueLine 生产领料单行
     * @return 生产领料单行
     */
    @Override
    public List<WmIssueLine> selectWmIssueLineList(WmIssueLine wmIssueLine)
    {

        QueryWrapper<WmIssueLine> queryWrapper = new QueryWrapper<>(wmIssueLine);
        queryWrapper.orderByDesc("line_id");
        return list(queryWrapper);
    }

    /**
     * 新增生产领料单行
     *
     * @param wmIssueLine 生产领料单行
     * @return 结果
     */
    @Override
    public SqlResult insertWmIssueLine(WmIssueLine wmIssueLine)
    {
        if (wmIssueLine.getIsInStock() == null){
            wmIssueLine.setIsInStock("1");
        }
        wmIssueLine.setCreateBy(SecurityUtils.getUsername());
        wmIssueLine.setCreateTime(DateUtils.getNowDate());
        return insertSQLModel(wmIssueLine);
    }

    /**
     * 修改生产领料单行
     *
     * @param wmIssueLine 生产领料单行
     * @return 结果
     */
    @Override
    public SqlResult updateWmIssueLine(WmIssueLine wmIssueLine)
    {
        wmIssueLine.setCreateBy(SecurityUtils.getUsername());
        wmIssueLine.setUpdateTime(DateUtils.getNowDate());
        return updateSQLModel(wmIssueLine);
    }

    /**
     * 批量删除生产领料单行
     *
     * @param lineIds 需要删除的生产领料单行主键
     * @return 结果
     */
    @Override
    public SqlResult deleteWmIssueLineByLineIds(Long[] lineIds)
    {
        return deleteSQLIds(lineIds);
    }

    /**
     * 删除生产领料单行信息
     *
     * @param lineId 生产领料单行主键
     * @return 结果
     */
    @Override
    public SqlResult deleteWmIssueLineByLineId(Long lineId)
    {
        WmIssueLine issueLine = getById(lineId);
        return deleteSQLModel(issueLine);
    }

    @Override
    public WmIssueLine findOld(WmIssueLine model) {
        return getById(model.getLineId());
    }

    @Override
    public List<WmIssueLine> getFromIds(Serializable[] ids) {
        QueryWrapper<WmIssueLine> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("line_id",ids);
        return list(queryWrapper);
    }

    //监听单领料单删除，相关的领料记录删除
    @SQLListenerInterface(tableName = "",listerType = {SQLListenerType.DeleteAfter})
    public SqlResult deleteByIssueHeaderId(Long issueId) {
        WmIssueLine wmIssueLine = new WmIssueLine();
        wmIssueLine.setIssueId(issueId);
        List<WmIssueLine> list = list(new QueryWrapper(wmIssueLine));
        return deleteSQLModelList(list);
    }

    @Override
    public List<GatherIssueZuZhuangInfo> findZuZhuangInfo(Long assembleIdToItemRecptLineId) {
        return getBaseMapper().findZuZhuangInfo(assembleIdToItemRecptLineId);
    }


//    //监听入库记录变化
//    @SQLListenerInterface(tableName = "wm_item_recpt_line",listerType = {SQLListenerType.BeforeAll})
//    public SqlResult wmItemRecptLineListener(SQLListenerType type, WmItemRecptLine oldObj, WmItemRecptLine newObj) {
//
//    }

}
